package com.permission_server.util.stpInterface;

import cn.dev33.satoken.stp.StpInterface;
import com.permission_server.entity.SysRolePermissionEntity;
import com.permission_server.entity.SysUserRoleEntity;
import com.permission_server.mapper.SysRolePermissionMapper;
import com.permission_server.mapper.SysUserRoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 自定义权限验证接口扩展
 */
@Component    // 保证此类被SpringBoot扫描，完成Sa-Token的自定义权限验证扩展
public class StpInterfaceImpl implements StpInterface {

    @Autowired
    SysUserRoleMapper sysUserRoleMapper;

    @Autowired
    SysRolePermissionMapper sysRolePermissionMapper;

    /**
     * 返回指定账号id所拥有的权限码集合
     *
     * @param loginId   账号id
     * @param loginType 账号类型
     * @return 该账号id具有的权限码集合
     */
    @Override
    public List<String> getPermissionList(Object loginId, String loginType) {
        ArrayList<String> list = new ArrayList<>();
        List<SysRolePermissionEntity> entityList = sysRolePermissionMapper.queryPermissionIdByAccount(loginId.toString());
        for (SysRolePermissionEntity entity : entityList) {
            list.add(entity.getPermissionId());
        }
        return list;
    }

    /**
     * 返回指定账号id所拥有的角色标识集合
     *
     * @param loginId   账号id
     * @param loginType 账号类型
     * @return 该账号id具有的角色标识集合
     */
    @Override
    public List<String> getRoleList(Object loginId, String loginType) {
        ArrayList<String> list = new ArrayList<>();
        List<SysUserRoleEntity> entityList = sysUserRoleMapper.queryRoleIdByAccount(loginId.toString());
        for (SysUserRoleEntity entity : entityList) {
            list.add(entity.getRoleId());
        }
        return list;
    }
}
